<?php
	/**
	*用户管理类
	**/
	class LogClass {
		// 构造函数
		public function __constract(){
		}

		// 获取日志列表
		public function getList($data){
			global $db;
			$pageCurrent = $data->pageCurrent;
			$pageSize = $data->pageSize;
			$userId = $data->userId;
			$startEndDate = $data->startEndDate;

			
			if ($userId != "all" && $startEndDate){
				// $userId 不为‘all’, $startEndDate不为null
				$db->query("select SQL_CALC_FOUND_ROWS
								log.id,
								user.name as user_name,
								menu.name as menu_name,
								log.data,
								log.create_time,
								log.IP
							from admin_log as log
								left join admin_user as user
								on log.userid=user.id
								left join admin_menu as menu
								on log.menuid=menu.id
								where 
									log.userid=:userId and
									log.create_time>:startDate and
									log.create_time<:endDate
								order by log.id desc
								limit :start,:count ", 
							array(
								"start"=>($pageCurrent-1) * $pageSize,
								"count"=> $pageSize,
								"userId"=>$userId,
								"startDate"=>$startEndDate[0],
								"endDate"=>$startEndDate[1]
								)
							);
			} else if ($userId == "all" && $startEndDate){
				// $userId 为‘all’, $startEndDate不为null
				$db->query("select SQL_CALC_FOUND_ROWS
								log.id,
								user.name as user_name,
								menu.name as menu_name,
								log.data,
								log.create_time,
								log.IP
							from admin_log as log
								left join admin_user as user
								on log.userid=user.id
								left join admin_menu as menu
								on log.menuid=menu.id
								where 
									log.create_time>:startDate and
									log.create_time<:endDate
								order by log.id desc
								limit :start,:count ", 
							array(
								"start"=>($pageCurrent-1) * $pageSize,
								"count"=> $pageSize,
								"startDate"=>$startEndDate[0],
								"endDate"=>$startEndDate[1]
								)
							);
			} else if ($userId != "all" && !$startEndDate){
				// $userId 为‘all’, $startEndDate为null
				$db->query("select SQL_CALC_FOUND_ROWS
								log.id,
								user.name as user_name,
								menu.name as menu_name,
								log.data,
								log.create_time,
								log.IP
							from admin_log as log
								left join admin_user as user
								on log.userid=user.id
								left join admin_menu as menu
								on log.menuid=menu.id
								where 
									log.userid=:userId
								order by log.id desc
								limit :start,:count ", 
							array(
								"start"=>($pageCurrent-1) * $pageSize,
								"count"=> $pageSize,
								"userId"=>$userId
								)
							);
			} else {
				// 所有日志信息
				$db->query("select SQL_CALC_FOUND_ROWS
								log.id,
								user.name as user_name,
								menu.name as menu_name,
								log.data,
								log.create_time,
								log.IP
							from admin_log as log
								left join admin_user as user
								on log.userid=user.id
								left join admin_menu as menu
								on log.menuid=menu.id
								order by log.id desc 
								limit :start,:count ", 
							array(
								"start"=>($pageCurrent-1) * $pageSize,
								"count"=> $pageSize
								)
							);
			}
			$logList = $db->prepare->fetchAll();

			// 获取总条数
			$db->query("select FOUND_ROWS() as total", "");
			$total = $db->prepare->fetchAll();
		
			$data = array(
					"total"=>$total[0]['total'],
					"data"=>$logList
				);

			return $data;
		}
	}
?>